1 + 1 # addition[1] 2
StartR Workshop
University of Konstanz
November 23, 2024
R is a programming language and software environment for statistical computing and graphics. Some of its main advantages are:
RStudio is an integrated development environment (IDE) for R and Python.
The console is R within RStudio. It provides the area to interactively execute code and get an immediate response. It’s typically used only for quick calculations that one doesn’t want to save.
> signals that R is waiting for your input.+ signals that R is waiting for you to complete your input.Press enter to submit your input or escape to cancel it.
Photo courtesy of @markusspiske
The console is ideally suited for quick calculations that are not saved.
Assignments are used in R to store information in objects.
the operator for assignments is <-
the notation for an assignment is object <- ...
to see the information stored in an object, call it by name
Tip
The shortut for the assignment operator is Alt + - (Windows) or Option + - (Mac).
R is case sensitive, so a and A would refer to different objects.
objects names can consist of letters (a-z, A-Z), numbers (0-9), and a few special characters like dots (.) and underscores (_)
if you call an object that does not exist, R will return an error
object names cannot start with a number or contain spaces
Objects can be used like variables.
They can be used repeatedly in the same calculation.
The object’s value does not change when it’s used like that.
To change an object, it must be assigned again.
Image: Getty Images
The women’s 100 m world record of 10.49 seconds was set by US athlete Florece Griffith-Joyner in 1988.
Store the her finishing time in an object called record.
Compute her speed in m/s and save it as speed_ms.
Compute her speed in km/h and save it as speed_kmh. (Note that \(km/h = m/s * 3.6\).)
The source pane is where you create and edit scripts for data processing and analysis.
.R extension.The code in a script is only evaluated after it was sent is sent to the console.
Ctrl + C) and paste (Ctrl + V) code into the consoleTip
The shortut for Run is Ctrl/Cmd + Enter.
Image: Getty Images
Open a new R script in RStudio.
Copy your solution of the last exercise (women’s 100 m world record) into the script.
Add comments to explain what each line of code does.
Save your script.
Run the script and check the results in the console.
Photo courtesy of @ruchindra
All objects created by an assignment are saved in the workspace.
Tip
The environment is useful for an overview of the available objects. Moreover, you can click on some objects like data frames to view them in a spreadsheet-like viewer.
The workspace (“global environment”) is where all objects created by an assignment are saved.
I recommend to deactivate automatic saving and restoring of the workspace because it can lead to unexpected results.
You can deactivate it in the Global Options under General - Workspace.
Photo courtesy of @seargreyson
The output pane provides access to several important features.
Packages are collections of R functions, datasets, help menus, and examples that extend the capabilities of Base R.
Install packages once with install.packages(), using quotation marks around the package name.
Load a package every time you start a new R session with library(). No quotation marks needed.
Use the :: operator (e.g., dplyr::filter()) to be specific about which package a function comes from.
Objects are one fundamental thing in R. The other fundamental thing are functions. The basic syntax of a function is
function_name(option1, option2, ...).
Functions are used to perform specific tasks. For example, the function mean() computes the mean of a vector of numbers. The result can be assigned to a new object.
Many functions are available in Base R. Additional functions are provided by packages and can be used after loading the package.
Photo courtesy of @chuttersnap
Install the package ggplot2 and load it into your current R session.
Get help about the ggsave() function. What does it do?
Comments (
#)A comments is text that is not evaluated as code. Comments are mainly used to explain what the code does. They are also helpful to structure scripts and for debugging.
Comments are preceded by a hash
#and can be placed on a line of their own or at the end of a line of code.